/**
* Copyright (C) 2007 Mustafa K. Isik - isik@acm.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

package com.paxcodi.rmi.helloworld;


import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class Hello_RMI_Client {

    /**
     * @param args hostname of server running rmi registry - format <code>rmi://host.domain:port/hello</code> - port defaults to 1099
     */
    public static void main(String[] args) {
	if (args.length == 0 || !args[0].startsWith("rmi://")) {
	    System.err.println("missing or malformed parameter - usage:\n" +
		    "java Hello_RMI_Client rmi://host.domain:port/hello\n" + 
	    "you may omit the port no in which case the standard RMI port 1099 will be addressed");
	    return;
	}
	
	try{
	    Object lookupObj = Naming.lookup(args[0]);
	    Hello_RMI_IF helloObj = (Hello_RMI_IF) lookupObj;
	    
	    String rcvHello = helloObj.sayHello();
	    System.out.println("server returned: " + rcvHello);
	} catch (MalformedURLException e) {
	    System.err.println(args[0] + " is not a valid RMI URL, see exception " + e);
	} catch (RemoteException e) {
	    System.err.println("Remote object threw exception " + e);
	} catch (NotBoundException e) {
	    System.err.println("Could not find the requested object on the server.");
	}
    }

}
